﻿using System.Text;

namespace Hjt212Parser
{
    public static class CrcZjhm
    {
        public static string CaluCrc(string paramString)
        {
            int i = 65535;
            int j = 0;
            int k = 1;
            int m = 40961;
            var databuff = Encoding.Default.GetBytes(paramString);
            int nLength = databuff.Length;
            for (int n = 0; n < nLength; n++)
            {
                i = databuff[n] ^ i;
                for (int i1 = 0; i1 < 8; i1++)
                {
                    j = i;

                    i = i >> 1;
                    if ((k & j) == 1)
                        i ^= m;
                }
            }
            return i.ToString("X4");
        }


        public static string CaluCrcV2(string paramString)
        {
            int i = 65535;
            int j = 0;
            int k = 1;
            int m = 40961;
            var databuff = Encoding.Default.GetBytes(paramString);
            int nLength = databuff.Length;
            for (int n = 0; n < nLength; n++)
            {
                i = databuff[n] << '\b' ^ i;
                for (int i1 = 0; i1 < 8; i1++)
                {
                    j = i;
                    i = i >> 1;
                    if ((k & j) == 1)
                        i ^= m;
                }
            }
            return i.ToString("X4");
        }

        public static string CaluCrcV212(string paramString)
        {
            int i = 65535;
            int j = 1;
            int k = 40961;
            var databuff = Encoding.Default.GetBytes(paramString);
            int nLength = databuff.Length;
            for (int m = 0; m < nLength; m++)
            {
                i = databuff[m] ^ i >> 8;
                for (int n = 0; n < 8; n++)
                    if ((j & i) == 1)
                        i = i >> 1 ^ k;
                    else
                        i >>= 1;
            }
            return i.ToString("X4");
        }

        public static string CaluCrc16(string paramString)
        {
            int i = 65535;
            int k = 1;
            int m = 40961;
            var databuff = Encoding.Default.GetBytes(paramString);
            int nLength = databuff.Length;
            for (int i2 = 0; i2 < nLength; i2++)
            {
                int n = i >> 8;
                int i1 = i & 0xFF;
                i1 = databuff[i2] ^ i1;
                i = (n << 8) + i1;
                for (int i3 = 0; i3 < 8; i3++)
                {
                    int j = i;
                    i = i >> 1;
                    if ((k & j) == 1)
                        i ^= m;
                }
            }
            return i.ToString("X4");
        }
    }
}
